﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MT4Trainer
{
    using System.IO;
    using System.Security.AccessControl;
    using System.Windows;
    using System.Windows.Interop;

    public partial class ScriptEditor : Form
    {
        public ScriptEditor()
        {
            InitializeComponent();
            //editor = GetNewEditor();
            //this.elementHost1.Child = editor;
        }

        private string EgaLocation = string.Empty;
        //    private SyntaxEditor editor { get; set; }
        public ScriptEditor(string text)
        {
            InitializeComponent();

            //editor = GetNewEditor();
            //this.elementHost1.Child = editor;
            //editor.Document = new EditorDocument();

            EgaLocation = text;
            using (StreamReader rd = new StreamReader(text))
            {
                string p = rd.ReadToEnd();
                richTextBox1.AppendText(p);
            }
        }



        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {


            Stream myStream;
            using (SaveFileDialog saveFileDialog1 = new SaveFileDialog())
            {
                saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                saveFileDialog1.FilterIndex = 2;
                saveFileDialog1.RestoreDirectory = true;
                saveFileDialog1.SupportMultiDottedExtensions = true;
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                   


                        EgaLocation = saveFileDialog1.FileName;
                       
                        //Lets write it all async..

                        File.WriteAllText(EgaLocation, richTextBox1.Text);

                        //Close the stream.
                      

                    
                }
            }
        }


        /// <summary>
        /// Opens a file.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <param name="stream">The <see cref="Stream"/> to load.</param>
        private void OpenFile(string filename)
        {
            using (var stream = new StreamReader(filename))
            {
                if (stream != null)
                {
                    if (this.richTextBox1 == null) richTextBox1.Clear();

                    this.richTextBox1.AppendText(stream.ReadToEnd());
                    EgaLocation = filename;
                }

            }
        }


        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (var opendiag = new OpenFileDialog() { Multiselect = false, RestoreDirectory = true })
            {
                if (opendiag.ShowDialog() == DialogResult.OK)
                {
                    OpenFile(opendiag.FileName);
                }
            }
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Lets clear the editor.
            //    this.editor.Text = string.Empty;
            this.richTextBox1.Clear();

        }

        private void ScriptEditor_Resize(object sender, EventArgs e)
        {
            //if the form is resized we resize the editor control.
            //this.elementHost1.Dock = DockStyle.Fill;
            //this.editor.Width = elementHost1.Width;
            //this.editor.Height = elementHost1.Height;
            //this.editor.InvalidateViews();

        }
    }
    public static class StringExtensions
    {

        public static MemoryStream ToMemoryStream(this string source)
        {
            return source.ToMemoryStream(Encoding.UTF8);
        }

        public static MemoryStream ToMemoryStream(

        this string source, Encoding encoding)
        {
            return new MemoryStream(encoding.GetBytes(source));
        }

    }


}
